#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash build.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] && echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] && echo "请输入正确的安装路径" && exit 1

create_dir() {
    local path=$1
    [[ "$path" =~ .*/$ ]] && path=${path%/*}
    if [[ ! -d "$path" && ! -f "$path" ]]; then mkdir -p $path; else path=$path$(date "+%y%m%d%H%M%S") && mkdir -p $path; fi
    echo $path
}

buildpath=$(create_dir $1)
installpath=$(create_dir $2)
yum install -y m4 environment-modules systemd-devel bc time
yum install -y cmake libcurl libcurl-devel openssl openssl-devel zlib zlib-devel bzip2 bzip2-devel xz-devel boost-devel bc htslib* gsl* ncurses ncurses-base ncurses-libs ncurses-devel samtools suitesparse-devel mariadb-devel sqlite-devel
source /etc/profile
module purge
set -e
dep_dir="dependency"
bash $dep_dir/bisheng-compiler-2.1.0.sh $buildpath $installpath
module use $installpath/bisheng-compiler-2.1.0
module load $installpath/bisheng-compiler-2.1.0/bisheng_modulefiles

bash $dep_dir/ncbi-blast-2.13.0.sh $buildpath $installpath
module use $installpath/ncbi-blast-2.13.0
module load $installpath/ncbi-blast-2.13.0/ncbi-blast_modulefiles

bash $dep_dir/lpsolve-5.5.sh $buildpath $installpath
module use $installpath/lp_solve_5.5
module load $installpath/lp_solve_5.5/lpsolve_modulefiles

bash $dep_dir/mysqlpp-3.3.0.sh $buildpath $installpath
module use $installpath/mysqlpp-3.3.0
module load $installpath/mysqlpp-3.3.0/mysqlpp_modulefiles

bash $dep_dir/bamtools-2.5.2.sh $buildpath $installpath
module use $installpath/bamtools-2.5.2
module load $installpath/bamtools-2.5.2/bamtools_modulefiles

bash $dep_dir/bcftools-1.9.sh $buildpath $installpath
module use $installpath/bcftools-1.9
module load $installpath/bcftools-1.9/bcftools_modulefiles

bash $dep_dir/augustus-3.4.0.sh $buildpath $installpath
module use $installpath/augustus-3.4.0
module load $installpath/augustus-3.4.0/augustus_modulefiles

bash $dep_dir/prodigal-2.6.3.sh $buildpath $installpath
module use $installpath/prodigal-2.6.3
module load $installpath/prodigal-2.6.3/prodigal_modulefiles

bash $dep_dir/metaeuk-r5.sh $buildpath $installpath
module use $installpath/metaeuk-r5
module load $installpath/metaeuk-r5/metaeuk_modulefiles

bash $dep_dir/hmmer-h3-arm.sh $buildpath $installpath
module use $installpath/hmmer
module load $installpath/hmmer/hmmer_modulefiles

bash busco-5.2.2.sh $buildpath $installpath

mkdir -p pkg/BUSCO-5.2.2-hpc && cp -r $installpath/* pkg/BUSCO-5.2.2-hpc/
cat >"pkg/BUSCO-5.2.2-hpc/run.sh" <<EOF
#!/bin/bash
yum install -y environment-modules tar python3-pip mariadb-devel boost-devel gsl-devel zlib-devel suitesparse-devel sqlite-devel python3-numpy python3-devel
yum -y install boost-iostreams.aarch64
yum -y install  boost-serialization.aarch64
yum install -y m4 systemd-devel csh time perl-Data-Dumper autoconf automake libtool numactl binutils valgrind flex wget tar libatomic gcc-c++ git
yum install -y cmake libcurl libcurl-devel openssl openssl-devel zlib bzip2 bzip2-devel xz-devel bc htslib* gsl* ncurses ncurses-base ncurses-libs ncurses-devel samtools
pip3 install pandas biopython
source /etc/profile &> /dev/null 
current_dir=\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" &> /dev/null && pwd)
for d in \$(ls \$current_dir)
do
    [[ ! -f "\$current_dir/\$d" ]] && cd \$current_dir/\$d && module use \$(pwd) && module load \$(pwd)/*modulefiles
done

EOF
chmod +x pkg/BUSCO-5.2.2-hpc/run.sh
cd pkg/ && tar zcvf BUSCO-5.2.2-hpc.tar.gz BUSCO-5.2.2-hpc

rm -rf $buildpath $installpath
